package com.novellotus.iov.carvice.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.OrderAlarmRecyclerAdapter;
import com.novellotus.iov.carvice.adapter.TimeAdapter;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;
import com.novellotus.iov.carvice.model.DiagnosisModel;
import com.novellotus.iov.carvice.utils.MyViewUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class DiagnosisActivity extends BaseActivity implements OrderAlarmRecyclerAdapter.OrderAlarmAdapterInterface, DiagnosisModel.DiagnosisInterface, TimeAdapter.TimeChooseInterface,LoadingView.OnBtnClickListener {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.recyclerTime)
    RecyclerView recyclerTime;
    @BindView(R.id.recyclerAlarm)
    RecyclerView recyclerAlarm;
    @BindView(R.id.loadingView)
    LoadingView loadingView;

    TimeAdapter timeAdapter;
    OrderAlarmRecyclerAdapter orderAlarmRecyclerAdapter;
    DiagnosisModel diagnosisModel;
    private String e = "", r = "", vin = "", plate = "", time = "";
    private RxDiagnosisAlarm rxDiagnosisAlarm;


    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_diagnosis);
    }

    @Override
    public void init() {
        if (getIntent().getExtras() != null) {
            vin = getIntent().getExtras().getString("vin");
            //  plate = getIntent().getExtras().getString("plate");
            e = getIntent().getExtras().getString("e");
            r = getIntent().getExtras().getString("r");
        }
        loadingView.setLoadingModel(LoadingView.MODEL_ALERT);
        loadingView.setOnBtnClickListener(this);
        loadingView.setBackgroundColor(this.getResources().getColor(R.color.white));
        tv_tittle.setText("自动化诊断");
        LinearLayoutManager linearLayoutManager = MyViewUtils.getLinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        diagnosisModel = new DiagnosisModel(this, this);
        diagnosisModel.getDiagnosisData(vin, "", e, r,loadingView);

        recyclerTime.setLayoutManager(linearLayoutManager);
        recyclerAlarm.setLayoutManager(MyViewUtils.getLinearLayoutManager(this));

    }


    @OnClick(R.id.img_back)
    void back() {
        finish();
    }


    @Override
    public void openElectricity() {
        Bundle bundleOrder = new Bundle();
        bundleOrder.putString("e", e);
        bundleOrder.putString("r", r);
        bundleOrder.putString("plate", plate);
        bundleOrder.putString("vin", vin);
        bundleOrder.putString("time", time);
        bundleOrder.putParcelable("alarm", rxDiagnosisAlarm);
        openActivity(DiagnosisDataActivity.class, bundleOrder);
    }

    @Override
    public void openCarData() {

    }

    @Override
    public void setData(RxDiagnosisAlarm rxDiagnosisAlarm) {
        this.rxDiagnosisAlarm = rxDiagnosisAlarm;
        if (rxDiagnosisAlarm != null) {
            if (timeAdapter == null) {
                timeAdapter = new TimeAdapter(rxDiagnosisAlarm.getTimeLines(), this);
                recyclerTime.setAdapter(timeAdapter);
            }
            orderAlarmRecyclerAdapter = new OrderAlarmRecyclerAdapter(rxDiagnosisAlarm, this, this);
            recyclerAlarm.setAdapter(orderAlarmRecyclerAdapter);
        }
    }

    @Override
    public void chooseTime(String time) {
        this.time = time;
        diagnosisModel.getDiagnosisData(vin, time, e, r,loadingView);
    }

    @Override
    public void onReload() {
        diagnosisModel.getDiagnosisData(vin, "", e, r,loadingView);
    }
}
